<template>
	<view>正在登录中</view>
</template>
<script>
	import store from '@/store';
	import api from '@/api/system/login.js';
	import util from '@/common/util/util.js';
	import systemConfig from '@/common/config/systemConfig.js';
	//不控制登录白名单
	const whiteList = ['login', 'noLoginError']
	export default {
		data() {
			return {
				spappurl: systemConfig.spappurl,
				gotoUrl: systemConfig.homeUrl,
				loginSussessQuery: {
					url: systemConfig.homeUrl,
					sp: ''
				},
				userType: '1',
				browserType: ''
			}
		},
		onLoad: function(option) {
			let browerInfo = store.state.hh_browerInfo;
			if (!browerInfo) {
				browerInfo = util.getBrowerInfo()
				this.$u.vuex('hh_browerInfo', browerInfo)
			}
			if (browerInfo) {
				this.browserType = browerInfo.browserType

			}
			console.log('browserType');
			console.log(this.browserType);
			if (this.browserType = 'zlb') {
				ZWJSBridge.getUserType().then((result) => {
					console.log(result);
					this.userType = result.userType;
					this.login(option);
				}).catch((error) => {
					console.log(error);
					this.login(option);
				});
				//获取用户类型 部分场景会卡死
				let that = this;
				setTimeout(function() {
					that.login(option);
				}, 3000)
			} else {
				this.login(option);
			}
		},
		methods: {
			login(option) {
				let token = store.state.hh_token
				let ignoreLogin = false

				let zlbFlag = true

				// #ifdef H5
				let accessToken = '' //本地登录token
				if (option.accessToken) {
					accessToken = option.accessToken
				} else {
					accessToken = util.getQueryString('accessToken')
				}
				let sp = '';
				if (option.sp) {
					sp = option.sp
				} else {
					sp = util.getQueryString('sp')
				}
				if (sp) {
					this.loginSussessQuery.url = sp;
				}
				let browerInfo = store.state.hh_browerInfo;
				if (!browerInfo) {
					browerInfo = util.getBrowerInfo()
					this.$u.vuex('hh_browerInfo', browerInfo)
				}
				let type = ''
				if (browerInfo) {
					type = browerInfo.browserType
					if (type == 'zlb' || type == 'zlb-alipay' || type == 'alipay') {
						// 浙里办 判断登录
						zlbFlag = true
						if (!store.state.hh_zlb_token) {
							//页面关闭后需要重新登录
							token = ''
						}
					}
				}
				//#endif
				if (zlbFlag) {
					ignoreLogin = false
				}
				// 判断页面是否登录或者忽略登录
				if (!ignoreLogin && !token) {
					if (zlbFlag) { //浙里办登录
						this.zlbLoginComponent();
					} else {
						this.gotoOther()
					}
				} else {
					this.loginSussessQuery.sp = '';
					this.gotoOther()
				}
			},
			async zlbLoginComponent() {
				if (ZWJSBridge.ssoTicket) {
					const ssoFlag = await ZWJSBridge.ssoTicket({});
					if (ssoFlag && ssoFlag.result === "true") {
						let ticket = ssoFlag.ticketId;

						if (ssoFlag.ticketId) {
							//根据ticketId 获取浙里办 token
							let stdata = {}
							stdata.st = ticket

							this.$http.post(api.wxGetUserByTicket, stdata).then((res) => {
								var Linstoken = res.result.accessToken;
								var expireMinute = res.result.expireMinute;
								var url = this.gotoUrl + "?accessToken=" + Linstoken;
								console.log("跳转首页：",url)
								window.location.replace(url);
							}).catch((error) => {
								this.getZlbTokenError(error, ticket);
							});
						} else {
							//当“浙里办”单点登录失败或登录态失效时调用 ZWJSBridge.openLink 方法重 新获取 ticketId。
							ZWJSBridge.openLink({
								type: "reload"
							}).then(res => {
								ticket = res.ticketId
							})
						}
					} else {
						console.log('使用 IRS【个人/法人单点登录】组件-2');
						//使用 IRS【个人/法人单点登录】组件
						this.loginComponent();
					}
				} else {
					console.log('使用 IRS【个人/法人单点登录】组件-1');
					//使用 IRS【个人/法人单点登录】组件
					this.loginComponent();
				}
			},
			loginComponent(flag, type) {
				console.log('here here here we are ');
				// alert('type type type:'+flag+type);
				if ((type = 'zlb')) {
					window.location.replace('https://puser.zjzwfw.gov.cn/sso/mobile.do?action=oauth&scope=1&servicecode=' +
						systemConfig.zlbCode + '&goto=' +
						encodeURIComponent(this.gotoUrl));
					/*window.location.href = 'https://puser.zjzwfw.gov.cn/sso/mobile.do?action=oauth&scope=1&servicecode=' +systemConfig.zlbCode + '&redirectUrl='
					    + encodeURIComponent( this.gotoUrl );*/
				} else {
					window.location.replace("https://puser.zjzwfw.gov.cn/sso/alipay.do?action=ssoLogin&servicecode=" +
						systemConfig.zlbCode + "&goto=" +
						encodeURIComponent(this.gotoUrl));
					/* window.location.href = "https://puser.zjzwfw.gov.cn/sso/alipay.do?action=ssoLogin&servicecode=" + systemConfig.zlbCode + "&redirectUrl="
					     + encodeURIComponent(this.gotoUrl);*/
				}
			},
			getZlbTokenError(error, ticket) {
				let queryData = {};
				if (error.message) {
					queryData.errorMsg = '浙里办票据认证异常:' + error.message + ticket;

				} else {
					if (error) {
						let errMsg = error.errMsg;
						if (errMsg.indexOf('timeout') != -1) {
							queryData.errorMsg = '网络超时';
						} else if (errMsg == 'Network Error') {
							queryData.errorMsg = '网络连接错误';
						} else {
							queryData.errorMsg = '浙里办票据认证异常' + JSON.stringify(error) + ticket;
						}
					} else {
						queryData.errorMsg = '浙里办票据认证异常' + ticket;
					}
				}
				this.$Router.replaceAll({
					path: '/pages/system/error/noLoginError',
					query: queryData,
				});
			},
			gotoOther(ticket) {
				console.log("跳转页面")
				let url = this.loginSussessQuery.url
				if (url) {
					let decNum = 0
					while (url.indexOf("/") < 0 && decNum < 4) {
						url = decodeURIComponent(url);
						decNum++;
					}
					let separator = url.indexOf('?') !== -1 ? "&" : "?";
					if (ticket) {
						url = url + separator + 'ticket=' + ticket + '&zlbLogin=true&accessToken=' + store.state.hh_token
					} else {
						url = url + separator + 'accessToken=' + store.state.hh_token
					}
					window.location.replace(url);
				}
			}
		}
	}

	//popstate
	window.addEventListener('popstate', function(event) {
		console.log('popstate');
		console.log(event);
		if (ZWJSBridge) {
			try {
				ZWJSBridge.close().then((result) => {
					console.log(result);
				}).catch((error) => {
					console.log(error);
				});
			} catch (e) {}
		}
		if (AlipayJSBridge) {
			try {
				AlipayJSBridge.call('closeWebview')
			} catch (e) {
				console.log(e)
			}
		}
	}, false);
	window.addEventListener('pageshow', function(event) {
		if (event.persisted || window.performance && window.performance.navigation.type == 2) {
			console.log('window.performance.navigation.type: ' + window.performance.navigation.type)
			if (ZWJSBridge) {
				try {
					ZWJSBridge.close().then((result) => {
						console.log(result);
					}).catch((error) => {
						console.log(error);
					});
				} catch (e) {}
			}
			if (AlipayJSBridge) {
				try {
					AlipayJSBridge.call('closeWebview')
				} catch (e) {
					console.log(e)
				}
			}
		}
	}, false);
</script>